package com.ssh.manager.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.Admin;
import com.ssh.entity.Company;
import com.ssh.entity.Product;
import com.ssh.entity.Task;
import com.ssh.entity.TaskImage;
import com.ssh.manager.AdminManager;
import com.ssh.manager.TaskManager;
import com.sun.org.apache.bcel.internal.generic.DALOAD;

@Service
public class TaskManagerImpl implements TaskManager {
	@Resource
	private IDao daoImpl;
	@Override
	public List<Task> getAll() {
		String sql = "from Task where del!=1 order by createDate desc";
		List<Task> tasks = (List<Task>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, -1, -1);
		return tasks;
	}

	@Override
	public boolean deleteImg(Long id) {
		try {
			TaskImage t = daoImpl.getDomain(TaskImage.class, id);
			t.setDel(1);
			daoImpl.saveOrUpdateDomain(t);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Task fingTaskById(Long id) {

		return daoImpl.getDomain(Task.class, id);
	}

	@Override
	public TaskImage saveUpdateImage(TaskImage taskImage) {
		return daoImpl.saveOrUpdateDomain(taskImage);
	}

	@Override
	public TaskImage findById_img(Long id) {
		return daoImpl.getDomain(TaskImage.class, id);
	}

	@Override
	public void findImgList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from TaskImage where del!=1";
		String findSql = "from TaskImage where del!=1";
		if(conditionMap.get("task")!=null && !conditionMap.get("task").equals("")){
			sql += " and taskBrand.task.content like '%"+conditionMap.get("task")+"%'";
			findSql += " and taskBrand.task.content like '%"+conditionMap.get("task")+"%'";
		}
		if(conditionMap.get("brand")!=null && !conditionMap.get("brand").equals("")){
			sql += " and taskBrand.brand.id="+conditionMap.get("brand");
			findSql += " and taskBrand.brand.id="+conditionMap.get("brand");
		}
		if(conditionMap.get("market")!=null && !conditionMap.get("market").equals("")){
			sql += " and superMarket.name like '%"+conditionMap.get("market")+"%'";
			findSql += " and superMarket.name like '%"+conditionMap.get("market")+"%'";
		}
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		findSql += " order by createDate "+isDesc;
		List<TaskImage> taskImages = (List<TaskImage>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(taskImages);
	}

	public void  findTaskByPage(Page page,String isDesc){
		
		String sql = "select count(*) from Task where del!=1";
		try {
			int companySum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(companySum));
			page.countTotalPageNum();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		sql = "from Task where del!=1 order by createDate "+isDesc;
		List<Task> tasks = (List<Task>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(tasks);
	}

	@Override
	public Task saveOrUpdateTask(Task task) {
		// TODO Auto-generated method stub
		return daoImpl.saveOrUpdateDomain(task);
	}

	public boolean deleteTask(Long id) {
		try {
			Task task = daoImpl.getDomain(Task.class, id);
			task.setDel(1);
			daoImpl.saveOrUpdateDomain(task);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
